;; Added by Package.el.  This must come before configurations of
;; installed packages.  Don't delete this line.  If you don't want it,
;; just comment it out by adding a semicolon to the start of the line.
;; You may delete these explanatory comments.
(package-initialize)


;; 猪场代理
;;-----------------------  Start -------------------------------------
(setq package-archives
        '(("melpa-cn" . "http://mirrors.163.com/elpa/melpa/")
          ("org-cn"   . "http://mirrors.163.com/elpa/org/")
          ("gnu-cn"   . "http://mirrors.163.com/elpa/gnu/")))
;;-----------------------  End -------------------------------------

;; 常规配置项
;;-----------------------  Start -------------------------------------
(setq make-backup-files nil) ;; 不要生成#开头的备份文件
(global-linum-mode t) ;; 显示行号
(tool-bar-mode -1) ;; disable tool-bar
;;(menu-bar-mode -1) ;; disable menu bar
;;(toggle-scroll-bar -1) ;; disable scroll bar

(add-hook 'c-mode-hook #'electric-operator-mode);操作符两边自动加空格
(add-hook 'python-mode-hook #'electric-operator-mode);操作符两边自动加空格
(add-hook 'go-mode-hook #'electric-operator-mode); electric-operator-mode不是一个全局模式,需要单独设定
(electric-pair-mode 1);自动补全括号
(show-paren-mode 1);配对括号高亮

;;-----------------------  End -------------------------------------


;; 主题相关
;;-----------------------  Start -------------------------------------
;; load all theme from replace-colorthemes folder
;; A ton of themes, very nice, see: https://github.com/emacs-jp/replace-colorthemes

;;;;;;; Tomorrow, A good theme manager ;;;;;;;;;;;;;;;;;
;; Github: https://github.com/purcell/color-theme-sanityinc-tomorrow
;;(require 'color-theme-sanityinc-tomorrow)
;; M-x color-theme-sanityinc-tomorrow
;; M-x color-theme-sanityinc-tomorrow-day
;; M-x color-theme-sanityinc-tomorrow-night
;; M-x color-theme-sanityinc-tomorrow-blue
;; M-x color-theme-sanityinc-tomorrow-bright
;; M-x color-theme-sanityinc-tomorrow-eighties
;; In newer Emacs versions, just require the library as above, then
;; M-x customize-themes


(load-theme 'tango-dark 1) ;; set theme , my favorite
;;(load-theme  'sanityinc-tomorrow-day 1) ;; set theme
;;(load-theme 'sanityinc-tomorrow-blue 1) ;; set theme ;; It is disable, because of above color-theme manager has take the setting.

(elpy-enable)

(setq custom-file (expand-file-name "custom.el" user-emacs-directory))
(unless (file-exists-p custom-file)
  (write-region "" nil custom-file))


;; 目录侧边栏
;;-----------------------  Start -------------------------------------
(global-set-key (kbd "<f8>") 'dired-sidebar-toggle-sidebar)
;; (use-package dired-sidebar
;;   :bind (("C-x C-n" . dired-sidebar-toggle-sidebar))
;;   :ensure t
;;   :commands (dired-sidebar-toggle-sidebar)
;;   :init
;;   (add-hook 'dired-sidebar-mode-hook
;;             (lambda ()
;;               (unless (file-remote-p default-directory)
;;                 (auto-revert-mode))))
;;   :config
;;   (push 'toggle-window-split dired-sidebar-toggle-hidden-commands)
;;   (push 'rotate-windows dired-sidebar-toggle-hidden-commands)

;;   (setq dired-sidebar-subtree-line-prefix "__")
;;   (setq dired-sidebar-theme 'vscode)
;;   (setq dired-sidebar-use-term-integration t)
;;   (setq dired-sidebar-use-custom-font t))
;;-----------------------  End -------------------------------------

;; For whitespcae-mode, 可以显示tab，空格等不可见符号
;;-----------------------  Start -------------------------------------
(global-set-key (kbd "<f9>") 'whitespace-mode)
;;-----------------------  End -------------------------------------

;; set 4 space instead of a tab
;;-----------------------  Start -------------------------------------
(setq-default indent-tabs-mode nil) ;; disable tab
(setq default-tab-width 4) 
(setq c-basic-offset 4) ;; set c/c++ indent 4
(setq c-default-style "linux") ;; if have no this
;;-----------------------  End -------------------------------------


;; for cscope
;;-----------------------  Start -------------------------------------
;;(load-file "~/.emacs.d/site-lisp")
(add-to-list 'load-path' "~/.emacs.d/site-lisp")
(add-hook 'c-mode-common-hook
	  '(lambda ()
	    (require 'xcscope)))
;;-----------------------  End -------------------------------------

;; for taglist
;;-----------------------  Start -------------------------------------
(add-hook 'c-mode-common-hook
	  '(lambda ()
	    (load "taglist")))
(defun my-c-mode-common-hook ()
 (define-key c-mode-base-map (kbd "M-o") 'taglist-switch-h-cpp)
 (define-key c-mode-base-map (kbd "M-m") 'taglist-list-tags))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

(defun my-python-mode-hook ()
 (define-key python-mode-map (kbd "M-m") 'taglist-list-tags))
(add-hook 'python-mode-hook 'my-python-mode-hook)

(define-key lisp-mode-shared-map (kbd "M-m") 'taglist-list-tags)
;;-----------------------  End -------------------------------------

;; For perl
;;-----------------------  Start -----------------------------------
(add-to-list 'company-backends 'company-plsense)
(add-hook 'perl-mode-hook 'company-mode)
(add-hook 'cperl-mode-hook 'company-mode)
;;-----------------------  End -------------------------------------

;; For Rust
;;-----------------------  Start -------------------------------------
;; irony-mode for c auto complete
;;(add-hook 'c++-mode-hook 'irony-mode)
;;(add-hook 'c-mode-hook 'irony-mode)
;;(add-hook 'objc-mode-hook 'irony-mode)
;;(add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)

(add-hook 'c-mode-hook 'auto-complete-mode)
;;(add-hook 'c-mode-hook 'show-paren-mode)

;; add in your commonly used packages/include directories here, for
;; example, SDL or OpenGL. this shouldn't slow down cpp, even if
;; you've got a lot of them
;;(add-hook 'c-mode-hook 'c-turn-on-eldoc-mode)
;;(add-hook 'c++-mode-hook 'c-turn-on-eldoc-mode)
;;-----------------------  End -------------------------------------


;; For Rust
;;-----------------------  Start -------------------------------------
;; Link https://github.com/rust-lang/rust-mode
(require 'rust-mode)

(add-hook 'rust-mode-hook
          (lambda () (setq indent-tabs-mode nil)))
(setq rust-format-on-save t)
(define-key rust-mode-map (kbd "C-c C-c") 'rust-run)

;; Link https://github.com/racer-rust/emacs-racer
(add-hook 'rust-mode-hook #'racer-mode)
(add-hook 'racer-mode-hook #'eldoc-mode)

(add-hook 'racer-mode-hook #'company-mode)
(add-hook 'flycheck-mode-hook #'flycheck-rust-setup)
(add-hook 'rust-mode-hook 'cargo-minor-mode)

(define-key rust-mode-map (kbd "C-x 4") 'racer-find-definition-other-frame)

(define-key rust-mode-map (kbd "TAB") #'company-indent-or-complete-common)
(setq company-tooltip-align-annotations t)
;;-----------------------  End -------------------------------------


;; lsp(language server protocol) (目前只用到imenu功能)
;; 显示文件内函数或变量
;; 自动调用imenu(生成当前文件缓冲器的符号索引，emacs自带)
;;-----------------------  Start -------------------------------------
;; 设置font-lock-mode，任何模式都会调用
(setq global-font-lock-mode t)

;; 在font-lock-mode-hook上调用imenu
;; 在顶部菜单栏显示
(defun try-to-add-imenu ()
  (condition-case nil (imenu-add-to-menubar "Imenu") (error nil)))
(add-hook 'font-lock-mode-hook 'try-to-add-imenu)

;; set default font
(set-frame-font "Ubuntu Mono Italic 14" nil t)

;; 自带imenu不好用，使用imenu-anywhere,从elpy安装
(global-set-key (kbd "C-c M-.") 'ivy-imenu-anywhere)


;; 又安装了lsp-ui, 其带有一系列工具，包括lsp-ui-imenu, 更好用
;; https://github.com/emacs-lsp/lsp-ui
(add-hook 'after-init-hook 'lsp-ui-mode) ;; 启动执行lsp-mode(还有其他lsp插件，进入lsp-ui-mode才能执行下面命令)
(global-set-key (kbd "M-.") 'lsp-ui-imenu)
;;-----------------------  End -------------------------------------------

;; show the git info in a directory
(add-hook 'dired-after-readin-hook 'dired-git-info-auto-enable)

;; elisp flycheck
(eval-after-load 'flycheck
  '(flycheck-package-setup))

; could be bad, will not let you save at all, until you correct the error
(add-hook 'emacs-lisp-mode-hook
  (lambda ()
   (add-hook 'local-write-file-hooks 
    'check-parens)))

;;-------------- by my self --------------
(defun cpp-highlight-if-0/1 ()
  "Modify the face of text in between #if 0 ... #endif."
  (interactive)
  (setq cpp-known-face '(background-color . "dim gray"))
  (setq cpp-unknown-face 'default)
  (setq cpp-face-type 'dark)
  (setq cpp-known-writable 't)
  (setq cpp-unknown-writable 't)
  (setq cpp-edit-list
        '((#("1" 0 1
             (fontified nil))
           nil
           (background-color . "dim gray")
           both nil)
          (#("0" 0 1
             (fontified nil))
           (background-color . "dim gray")
           nil
           both nil)))
  (cpp-highlight-buffer t))

(defun jpk/c-mode-hook ()
  (cpp-highlight-if-0/1)
  (add-hook 'after-save-hook 'cpp-highlight-if-0/1 'append 'local)
  )

(add-hook 'c-mode-common-hook 'jpk/c-mode-hook)
;;(add-hook 'c++-mode-hook 'jpk/c-mode-hook)
;;(add-hook 'c-mode-hook 'jpk/c-mode-hook)

;;(shell-command "ls && pwd && git status && tree")
(defun tete-build-bes (projname)
  "To Build icode bes project"
  (interactive "sPlease type the project name to build: ")
  (progn
    (shell)
    (set-buffer (get-buffer "*shell*"))
    (if (string= projname "bes")
        (insert "cd /media/zhangte/disk_1T/bes/baidu/duer/lightduer-bes2300-sdk/software/m33 && zsh ./gen_factory_package.sh B101 | tee compil.log")
      (if (string= projname "xly")
          (insert "cd /media/zhangte/disk_1T/bes/baidu/duer/xly-bes2300_sdk/software/m33 && zsh ./gen_factory_package.sh B101 | tee compil.log")
        (if (string= projname "bes2")
            (insert "cd /media/zhangte/disk_1T/bes/baidu/duer/lightduer-bes2300-sdk2/software/m33 && zsh ./gen_factory_package.sh B101 | tee compil.log"))))
    (comint-send-input)))
(global-set-key (kbd "C-c e") 'tete-build-bes)

(defun tab/space-style ()
  "To switch indent"
  (interactive nil)
  (if (null indent-tabs-mode)
      (progn (setq indent-tabs-mode t) ;; disable tab
             (setq tab-width 8)
             (message "switch to tab style")
             (setq c-basic-offset 8) ;; set c/c++ indent 4
             (setq c-default-style "linux"))
    (progn (setq indent-tabs-mode nil) ;; disable tab
           (setq tab-width 4)
           (message "switch to 4 space style")
           (setq c-basic-offset 4) ;; set c/c++ indent 4
           (setq c-default-style "linux"))
    ))
(global-set-key (kbd "C-c t") 'tab/space-style)

(defun close-all-buffers ()
  (interactive nil)
  (delete (current-buffer) (buffer-list))
  (mapc 'kill-buffer (buffer-list)))

(defun close-other-buffers ()
    "Close all other buffers."
    (interactive)
    (mapc 'kill-buffer 
          (delq (current-buffer) 
                (remove-if-not 'buffer-file-name (buffer-list)))))

(global-set-key (kbd "C-c k") 'close-all-buffers)
(global-set-key (kbd "C-c o") 'close-other-buffers)

(defun show-ibuffer ()
    "Open the ibuffer."
    (interactive)
    (ibuffer))
(global-set-key (kbd "C-c i") 'show-ibuffer)

;; enable ascii-color by M-x
(require 'ansi-color)
(defun my-ansi-color ()
    (interactive)
    (ansi-color-apply-on-region (point-min) (point-max)))

(put 'scroll-left 'disabled nil)

;; Config of multiple-cusors
(require 'multiple-cursors)

(global-set-key (kbd "C-S-c C-S-c") 'mc/edit-lines)
(global-set-key (kbd "C->") 'mc/mark-next-like-this)
(global-set-key (kbd "C-<") 'mc/mark-previous-like-this)
(global-set-key (kbd "C-c C-<") 'mc/mark-all-like-this)

(setq defult-input-method "chinese-py")
(global-set-key (kbd "C-\\") 'toggle-input-method)

